package gw.com.android.ui.dialog;


import android.app.Activity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import www.com.library.dialog.BasePopWindow;

/**
 * 照片弹窗
 * @author hubble
 */
public class SelectPhotoPopWindow extends BasePopWindow implements OnClickListener{
	private TextView mTvTakePhoto;
	private TextView mTvSelectImg;
	private TextView mTvCancel;

	private OnItemClickListener listener;
	
	public SelectPhotoPopWindow(Activity activity, View view) {
		initPopWindow(activity, view, R.layout.view_select_photo_popview,null);
		// 高度自适应
		popWindow.setTouchable(true);

		popWindow.setTouchInterceptor(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				return false;
				// 这里如果返回true的话，touch事件将被拦截
				// 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
			}
		});
	}
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.tv_cancel) {
			/*隐藏弹窗*/
			listener.onCancel();
			popWindow.dismiss();
		}else if(id == R.id.tv_take_photo) {
			listener.onCamera();
			popWindow.dismiss();

		}else if(id == R.id.tv_select_img) {
			listener.onPick();
			popWindow.dismiss();
		}
	}

	public void setOnItemClickListener(OnItemClickListener listener) {
		this.listener = listener;
	}


	@Override
	public void show() {
		if (hasShowing()) {
			return;
		}
		if (!activity.isFinishing()) {
			view = activity.getWindow().getDecorView();
			popWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
			backgroundAlpha(activity,0.7f);
		}
	}

	@Override
	public void createPopWindow(View dialogView) {
		mTvCancel = (TextView) dialogView.findViewById(R.id.tv_cancel);
		mTvSelectImg = (TextView) dialogView.findViewById(R.id.tv_select_img);
		mTvTakePhoto = (TextView) dialogView.findViewById(R.id.tv_take_photo);
		mTvTakePhoto.setOnClickListener(this);
		mTvSelectImg.setOnClickListener(this);
		mTvCancel.setOnClickListener(this);
	}

	public interface OnItemClickListener {
		void onCamera();
		void onPick();
		void onCancel();
	}
}
